package com.zeng.configuration.microprofile.config.source.servlet;


import com.zeng.configuration.microprofile.config.source.MapBasedConfigSource;

import javax.servlet.ServletContext;
import java.util.*;

/**
 * @author zyb
 */
public class ServletContextConfigSource extends MapBasedConfigSource {


    public ServletContextConfigSource(ServletContext servletContext) {
        super("ServletContext Init Parameters", 500, new HashMap<>(prepareConfigData(servletContext)));
    }


    private static Map<String, String> prepareConfigData(ServletContext servletContext) {
        List<String> list = Collections.list(servletContext.getInitParameterNames());
        Map<String, String> map = new HashMap<>(list.size());
        for (String parameterName : list) {
            map.put(parameterName, servletContext.getInitParameter(parameterName));
        }
        return map;
    }

    @Override
    protected Map<String, String> prepareConfigData() throws Throwable {
        throw new UnsupportedOperationException();
    }
}
